//
//  LeftMenuVC.m
//  MyDemo
//
//  Created by wsj2015 on 2016/12/6.
//  Copyright © 2016年 macOS. All rights reserved.
//

#import "LeftMenuVC.h"
#import "LoginVC.h"
#import "Singleton.h"
#import "MBProgressHUD.h"
#import "WCPopupLayerManager.h"
#import "AFNetworking.h"
//更换头像
NSString * iconActionNotification = @"iconActionNotification";
//登录
NSString * loginActionNotification = @"loginActionNotification";
//注册
NSString * registerActionNotification = @"registerActionNotification";
//退出
NSString * logoutActionNotification = @"logoutActionNotification";
@interface LeftMenuVC ()
@property (nonatomic, strong) UIButton *btn1;
@property (nonatomic, strong) Singleton *center;
@property (nonatomic, strong) LoginVC *lvc;
@end

@implementation LeftMenuVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _center = [Singleton sharedSingleton];
    self.view.backgroundColor = [UIColor whiteColor];
    
    //头像
    UIButton *btn0 = [UIButton buttonWithType:UIButtonTypeSystem];
    [btn0 setBackgroundImage:[UIImage imageNamed:@"header_icon"] forState:UIControlStateNormal];
    [btn0 addTarget:self action:@selector(iconAction:) forControlEvents:UIControlEventTouchUpInside];
    btn0.frame = CGRectMake(8, 28, 64, 64);
    btn0.layer.cornerRadius = 32;
    btn0.layer.masksToBounds = YES;
    [self.view addSubview:btn0];
    
    //登录
    _btn1 = [UIButton buttonWithType:UIButtonTypeSystem];
    //初始化 (登录/已登录)
    NSString *btn1_title = ((_center.isLogin)?(@"已登录"):(@"登录"));
    [_btn1 setTitle:btn1_title forState:0];
    _btn1.enabled = !_center.isLogin;
    [_btn1 setTitleColor:[UIColor whiteColor] forState:0];
    _btn1.backgroundColor = [UIColor grayColor];
    [_btn1 addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];
    _btn1.frame = CGRectMake(8, 28+64+15, 64, 32);
    _btn1.layer.cornerRadius = 2;
    _btn1.layer.masksToBounds = YES;
    [self.view addSubview:_btn1];
    
    //退出
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeSystem];
    [btn2 setTitle:@"退出" forState:0];
    [btn2 setTitleColor:[UIColor whiteColor] forState:0];
    btn2.backgroundColor = [UIColor grayColor];
    [btn2 addTarget:self action:@selector(logoutAction:) forControlEvents:UIControlEventTouchUpInside];
    btn2.frame = CGRectMake(8, 28+64+15+32+15, 64, 32);
    btn2.layer.cornerRadius = 2;
    btn2.layer.masksToBounds = YES;
    [self.view addSubview:btn2];
    
}

//点击更换头像
- (void)iconAction:(id)sender{
    Log(@"点击更换头像");
    
    //[self postNoti:@"点击更换头像"  notiName:iconActionNotification];
    
}

//点击登录
- (void)loginAction:(id)sender{
    Log(@"点击登录");
    
    if ([Singleton sharedSingleton].isLogin == YES) {
        return;
    }
    //  [self postNoti:@"点击登录" notiName:loginActionNotification];
    _lvc = [LoginVC new];
    [self presentViewController:_lvc animated:YES completion:nil];
    
    __weak typeof (self)weakSelf = self;
    _lvc.blockIsSuccessLoginHandler = ^{
        [weakSelf.btn1 setTitle:@"已登录" forState:0];
        weakSelf.btn1.enabled = NO;
    };
    
    
}

//点击退出
- (void)logoutAction:(id)sender{
    Log(@"点击退出");
    if ([Singleton sharedSingleton].isLogin == NO) {
        return;
    }
    //[self postNoti:@"点击退出" notiName:logoutActionNotification];
    [self centerLogout];
    
  
    
}

- (void)centerLogout
{
    
    
    MBProgressHUD *hud = [[WCPopupLayerManager shareMg] showSimple:[UIApplication sharedApplication].keyWindow];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSDictionary *param = @{
                            @"token":[[NSUserDefaults standardUserDefaults]objectForKey:@"token"]
                            };
    __weak typeof (self)weakSelf = self;
    //获取用户信息
    [manager GET:@"http://103.13.222.30/api/v3/logout"
      parameters:param
        progress:nil
         success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
             [hud hide:YES];
             [[NSUserDefaults standardUserDefaults]setObject:@"logout" forKey:@"userInfoData"];
             [[NSUserDefaults standardUserDefaults]setObject:@"logout" forKey:@"token"];
             _center.isLogin = NO;
             [_btn1 setTitle:@"登录" forState:0];
             _btn1.enabled = YES;
             [[WCPopupLayerManager shareMg] showTextOnly:responseObject[@"msg"] withView:[UIApplication sharedApplication].keyWindow];
             [weakSelf dismissViewControllerAnimated:YES completion:nil];
             
         } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
             [hud hide:YES];
             //hud.detailsLabelText = error.localizedDescription;
             [[WCPopupLayerManager shareMg] showTextOnly:error.localizedDescription withView:[UIApplication sharedApplication].keyWindow];

         }];
    
}

- (void)postNoti:(NSString *)message  notiName:(NSString *)name {
    NSDictionary *nitiDic =[[NSDictionary alloc] initWithObjectsAndKeys:message,@"NotiKey",nil];
    NSNotification *notification =[NSNotification notificationWithName:name object:nil userInfo:nitiDic];
    [[NSNotificationCenter defaultCenter] postNotification:notification];
}

@end
